package com.inkyi.assistant.app.tools.comps;

import com.inkyi.assistant.app.tools.base.InkTools;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.time.LocalDate;

/**
 * 复制密码工具
 *
 * @author lihongkun
 * @date 2025/08/06
 */
public class NovaGeneralPwdTool extends JPanel implements InkTools {

    private JButton copyButton;

    private JPasswordField pwdPrefix;

    private JLabel remark;

    public NovaGeneralPwdTool() {
        initComponents();
    }

    @Override
    public void initComponents() {

        setLayout(new FlowLayout(FlowLayout.LEFT));

        pwdPrefix = new JPasswordField();
        pwdPrefix.setText("wmqegcjstypwd");
        copyButton = new JButton("复制密码");
        remark = new JLabel();

        add(pwdPrefix);
        add(copyButton);
        add(remark);

        copyButton.addActionListener(e -> {
            String passwordPrefix = pwdPrefix.getText().trim();
            int dayOfMonth = LocalDate.now().getDayOfMonth();
            StringSelection stringSelection = new StringSelection(passwordPrefix + dayOfMonth);
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, null);
            remark.setText("复制成功");
            Timer clearTimer = new Timer(5 * 1000, e1 -> {
                remark.setText("");
            });
            clearTimer.setRepeats(false);
            clearTimer.start();
        });
    }

    @Override
    public String getToolCode() {
        return "nova-ceneral-pwd";
    }

    @Override
    public String getToolName() {
        return "销售观通用密码";
    }

    @Override
    public String getToolID() {
        return "tools-" + getToolCode();
    }

    @Override
    public JComponent getComponent() {
        return this;
    }
    @Override
    public String getOpenType() {
        return "workspace";
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        NovaGeneralPwdTool panel = new NovaGeneralPwdTool();
        panel.initComponents();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
}
